import 'dart:async';

import 'package:flutter/material.dart';
import 'package:helloworld/res/payData.dart';
import 'package:helloworld/tabs.dart';
import 'package:flutter_cupertino_date_picker/flutter_cupertino_date_picker.dart';
import 'package:date_format/date_format.dart';
import 'package:dio/dio.dart';
import 'dart:ui';
class PayDetail extends StatefulWidget{
  final arguments;
  PayDetail({this.arguments});
  @override
  State<StatefulWidget> createState() {
    // TODO: implement createState
    return _PayDetail(arguments:this.arguments);
  }
}

class _PayDetail extends State<PayDetail>{
  var _name = TextEditingController();
  var _nameData;
  var _date = TextEditingController();
  DateTime _dateData ;
  var _pay = TextEditingController();
  var _payData;
  final arguments;
  Brightness brightnessValue ;
  _PayDetail({this.arguments});
  _delete(){
    showDialog(context: context,builder: (context){
      return AlertDialog(
        content: Text('您确定要删除吗?',style: TextStyle(fontSize: 20),),
        actions: <Widget>[
          FlatButton(
            child: Text('取消',style: TextStyle(fontSize: 20),),
            onPressed: (){
              Navigator.pop(context);
            },
          ),
          FlatButton(
            child: Text('确定',style: TextStyle(fontSize: 20),),
            onPressed: (){
              print(payData[this.arguments['id']]['payid']);
              Navigator.pop(context);
              Dio().post("http://123.207.114.127:1314/pay/deletePay",queryParameters: {
                'payid':payData[this.arguments['id']]['payid']
              });
              Future.delayed(Duration(milliseconds: 50), (){
                Navigator.of(context).pop();
                Navigator.of(context).pushAndRemoveUntil(new MaterialPageRoute(builder: (context) => new Tabs( index:0)), (route) =>route==null);
              });
            },
          ),

        ],
      );
    });
  }
  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    _name.text = payData[this.arguments['id']]['payname'];
    _nameData = payData[this.arguments['id']]['payname'];
    _date.text = payData[this.arguments['id']]['paydate'];
    _dateData = null;
    _pay.text = (-payData[this.arguments['id']]['pay']).toStringAsFixed(2);
    _payData = (-payData[this.arguments['id']]['pay']).toStringAsFixed(2);
    brightnessValue = window.platformBrightness;
  }
  @override
  Widget build(BuildContext context) {
    // TODO: implement build
    return Scaffold(
      appBar: AppBar(
        elevation: 0,
        title: Text('编辑支出'),
        actions: <Widget>[
          InkWell(
            child: Padding(
              child:Text('保存',style: TextStyle(color: Colors.white,fontSize: 20),),
              padding: EdgeInsets.all(16),
            ),
            onTap: (){
              String time = '';
              String paymonth = '';
              String payday = '';
              String payyear = '';
              if(_dateData==null){
                time = payData[this.arguments['id']]['paydate'];
                paymonth = payData[this.arguments['id']]['paymonth'];
                payday = payData[this.arguments['id']]['payday'];
                payyear = payData[this.arguments['id']]['payyear'];
              }else{
                time = '${formatDate(_dateData, [mm, '月',dd ,'日'])}';
                paymonth = '${formatDate(_dateData, [mm, ])}';
                payday = '${formatDate(_dateData, [dd ])}';
                payyear = '${formatDate(_dateData, [yyyy, ])}';
              }
              Dio().post("http://123.207.114.127:1314/pay/updatePay",queryParameters: {
                'payid':payData[this.arguments['id']]['payid'],
                'payname':_nameData,
                'paydate':time,
                'pay': -double.parse(_payData),
                'paymonth':paymonth,
                'payday':payday,
                'payyear':payyear
              });
              Future.delayed(Duration(milliseconds: 50), (){
                Navigator.of(context).pop();
                Navigator.of(context).pushAndRemoveUntil(new MaterialPageRoute(builder: (context) => new Tabs( index:0)), (route) =>route==null);
              });
            },
          )
        ],
      ),
      body:  Padding(
        padding: EdgeInsets.all(20),
        child: SingleChildScrollView(
          child: Column(
            children: <Widget>[
              TextField(
                keyboardAppearance: brightnessValue,
                controller: _name,
                decoration: InputDecoration(
                    border: OutlineInputBorder(),
                    labelText: '事件'
                ),
                onChanged: (value){
                  setState(() {
                    _nameData = value;
                  });

                },
              ),
              SizedBox(height: 10,),
              InkWell(
                child: TextField(
                  enabled: false,
                  controller: _date,
                  decoration: InputDecoration(
                      border: OutlineInputBorder(),
                      labelText: '日期'
                  ),
                ),
                onTap: (){
                  DatePicker.showDatePicker(
                    context,
                    onMonthChangeStartWithFirstDate: true,
                    pickerTheme: DateTimePickerTheme(
                      showTitle: true,
                      confirm: Text('确定', style: TextStyle(color: Colors.black54,fontSize: 20)),
                      cancel: Text('取消', style: TextStyle(color: Colors.black54,fontSize: 20)),
                    ),
                    minDateTime: DateTime.parse('2020-01-01'),
                    maxDateTime: DateTime.parse('2099-12-31'),
                    initialDateTime: DateTime.now(),
                    dateFormat: 'MMMM-dd',
                    locale: DateTimePickerLocale.zh_cn,
                    onConfirm: (dateTime, List<int> index) {
                      setState(() {
                        _dateData = dateTime;
                        _date.text = '${formatDate(_dateData, [mm, '月',dd ,'日'])}';
                      });
                    },
                  );
                },
              ),
              // 10
              SizedBox(height: 10,),
              TextField(
                keyboardAppearance: brightnessValue,
                keyboardType: TextInputType.numberWithOptions(decimal: true),
                controller: _pay,
                decoration: InputDecoration(
                    border: OutlineInputBorder(),
                    labelText: '金额'
                ),
                onChanged: (value){
                  setState(() {
                    _payData = value;
                  });

                },
              ),
              SizedBox(height: 50,),
              Container(
                width: 150,
                height: 49,
                child: RaisedButton(
                  color: Colors.lightBlueAccent,
                  textColor: Colors.white,
                  child: Text('删除',style: TextStyle(fontSize: 20),),
                  onPressed: _delete,
                ),
              )

            ],
          ),
          scrollDirection: Axis.vertical,
        ),
      ),
    );
  }
}